﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Librarian.Shared;

namespace Librarian
{
    public partial class FilterControl : UserControl
    {
        bool _collapsed=true;

        public bool Collapsed { get { return _collapsed; } set { _collapsed = value; refreshState(); } }

        public string FilterText { get { return this.textBox1.Text; } }

        public EzSearchOptions SearchOptions { get; set; }

        public YearFilter YearFilter
        {
            get
            {
                YearFilter value= null;
                if (!string.IsNullOrEmpty((string)comboBox1.SelectedItem))
                {
                    value = new YearFilter() { MinYear = (int)numericUpDown1.Value, MaxYear = (int) numericUpDown2.Value };
                    switch ((string)comboBox1.SelectedItem)
                    {
                        case ">=":
                            value.DateRange = Comparison.GreaterThan;
                            break;
                        case "<=":
                            value.DateRange = Comparison.SmallerThan;
                            break;
                        case "=":
                            value.DateRange = Comparison.Equal;
                            break;
                        case "Entre":
                            value.DateRange = Comparison.Between;
                            break;
                    }
                }
                return value;
            }
        }


        public event EventHandler SearchClick
        {
            add { this.findButton.Click += value; }
            remove { this.findButton.Click -= value; }
        }

        public event EventHandler ClearClick
        {
            add { this.clearButton.Click += value; }
            remove { this.clearButton.Click -= value; }
        }

        public FilterControl()
        {
            InitializeComponent();
            refreshState();
            SearchOptions = EzSearchOptions.All;
        }

        private void refreshState()
        {
            this.tabControl1.Visible = !_collapsed;
            this.Height = _collapsed ? 29 : 141;
            this.pictureBox1.Image=imageList1.Images[_collapsed ? 0 : 1];
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                findButton.PerformClick();
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            _collapsed = !_collapsed;
            refreshState();
        }


        bool changing;
        private void toggle(EzSearchOptions ezSearchOptions)
        {
            if (!changing)
            {
                changing = true;

                SearchOptions ^= ezSearchOptions;

                checkBoxAll.Checked = SearchOptions.HasValue(EzSearchOptions.All);
                checkBoxAuthor.Checked = SearchOptions.HasValue(EzSearchOptions.Author);
                checkBoxComments.Checked = SearchOptions.HasValue(EzSearchOptions.Comments);
                checkBoxKeyword.Checked = SearchOptions.HasValue(EzSearchOptions.Keyword);
                checkBoxLocation.Checked = SearchOptions.HasValue(EzSearchOptions.Location);
                checkBoxTheme.Checked = SearchOptions.HasValue(EzSearchOptions.Theme);
                checkBoxTitle.Checked = SearchOptions.HasValue(EzSearchOptions.Title);
                checkBoxType.Checked = SearchOptions.HasValue(EzSearchOptions.Type);

                changing = false;
            }
        }

        private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
        {
            if (!changing)
            {
                if (checkBoxAll.Checked)
                    SearchOptions = EzSearchOptions.None;
                else
                    SearchOptions = EzSearchOptions.All;
            }

            toggle(EzSearchOptions.All);
        }

        private void checkBoxTitle_CheckedChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Title);
        }


        private void checkBoxAuthor_CheckedChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Author);
        }

        private void checkBoxComments_CheckedChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Comments);
        }

        private void checkBoxKeyword_CheckedChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Keyword);
        }

        private void checkBoxLocation_CheckedChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Location);
        }

        private void checkBoxTheme_CheckedChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Theme);
        }

        private void checkBoxType_CheckedChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Type);
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            checkBoxAll.Checked = true;
            textBox1.Text = "";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty((string)comboBox1.SelectedItem))
            {
                numericUpDown1.Enabled = false;
            }
            else
            {
                numericUpDown1.Enabled = true;
            }

            if ((string)comboBox1.SelectedItem == "Entre")
            {
                numericUpDown2.Enabled = true;
            }
            else
                numericUpDown2.Enabled = false;

        }

    }
}
